// gltx
package glutil

import (
	"fmt"

	"gitee.com/liuyl162/utils/crcutil"
)

type Txml interface {
	ResetCrc()
	GetReq() []byte
	IsValidRes(buf []byte) error
}

//通讯命令
type TxmlBase struct {
	ReqBuf []byte //请求报文
	HasRes bool   //是否有回复
	ResBuf []byte //响应报文
	ResLen int    //响应字节数
}

func (o *TxmlWs) IsValidRes(buf []byte) error {
	if o.ResLen <= 0 { //无回复
		return nil
	}
	if l := len(buf); l < o.ResLen {
		return fmt.Errorf("回复数据不完整:%d<%d", l, o.ResLen)
	}

	if l := len(buf); l < o.ResLen {
		return fmt.Errorf("回复数据不完整:%d<%d", l, o.ResLen)
	}
}

//握手
type TxmlWs struct {
	TxmlBase
}

func (o *TxmlWs) Build(addr byte) {
	o.Req = []byte{addr, 0x80}
	o.Req = crcutil.AppendModbusCrc16(o.Req)
	o.Res = []byte{addr, 0x4f, 0x4b}
}

func TxReq_ws(addr byte) {
	req := []byte{addr, 0x80}
}
func (o *TxmlWs) IsValidRes() {
	req := []byte{addr, 0x80}
}
